﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace TST.SharePointObjects
{
    internal class FeatureDefinitionFarmReferences : ObjectReferenceList
    {
        public FeatureDefinitionFarmReferences(LicensingMode licensingMode)
            : base(licensingMode)
        {
        }

        public override ObjectReferenceType ObjectReferenceType
        {
            get { return ObjectReferenceType.FeatureDefinitionFarm; }
        }

        internal void Add(SiteObjectList objects, SPFarm farm)
        {
            String adminUrl = String.Empty;
            Microsoft.SharePoint.Administration.SPAdministrationWebApplication centralWeb = SPAdministrationWebApplication.Local;
            using (SPSite centralAdmin = centralWeb.Sites[0])
            {
                adminUrl = centralAdmin.Url;
            }
            foreach (SPFeature feature in SPAdministrationWebApplication.Local.WebService.Features)
            {
                SiteObject siteObject = objects.GetItemById(feature.DefinitionId.ToString("B"));
                if (siteObject != null && siteObject.Index)
                {
                    string refId = Globals.GetReferenceId(siteObject, feature);
                    ObjectReference newReference = new ObjectReference(farm.Id.ToString("B"), farm.DisplayName, refId, ObjectReferenceType.FeatureDefinitionFarm);
                    newReference.Url = string.Format("{0}/_admin/ManageFarmFeatures.aspx", adminUrl);
                    newReference.SiteTitle = String.Empty;
                    newReference.SiteUrl = String.Empty;
                    newReference.ListTitle = String.Empty;
                    newReference.ListUrl = String.Empty;
                    Add(newReference);
                }
            }
        }

    }
}
